package cn.edu.scau.cmi.hekaimin.Hibernate.controller;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import cn.edu.scau.cmi.hekaimin.Hibernate.dao.StudentDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Student;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.text.Text;

public class TeacherFXController {
	@FXML
	private Text text_1;
	@FXML
	private TableView tableSelected;
	@FXML
	private TableView tableOption;
	@FXML
	private Button btn_delete;
	@FXML
	private Button btn_add;
	@FXML
	private TableColumn leftID;
	@FXML
	private TableColumn leftName;
	@FXML
	private TableColumn rightID;
	@FXML
	private TableColumn rightName;
    Teacher teacher = null;
    private ObservableList<Student> studentLeft = FXCollections.observableArrayList();
    private ObservableList<Student> studentRight = FXCollections.observableArrayList();
	public void init() {
		TeacherDao tea = new TeacherDao();
		StudentDao stu = new StudentDao();
		List<Student> students = stu.getAll();
		List<Teacher> teachers = tea.getAll();
		
		Random random = new Random();
		int n = random.nextInt(teachers.size()); //随机获取Teacher对象值
		teacher = teachers.get(n);
		text_1.setText("欢迎" + teacher.getT_name() + "老师登录!");
		Set<Student> selected = teacher.getStudents();
		for(Student e:selected) {
			studentLeft.add(e);         //获取student已选值
		}
		for(Student e : students) {
			if(e.getTeacher() == null) {
				studentRight.add(e);    //获取teacher未选值
			}
		}
		leftID.setCellValueFactory(new PropertyValueFactory("s_id"));
		leftName.setCellValueFactory(new PropertyValueFactory("s_name"));
		
		rightID.setCellValueFactory(new PropertyValueFactory("s_id"));
		rightName.setCellValueFactory(new PropertyValueFactory("s_name"));
		
		tableSelected.setItems(studentLeft);
		tableOption.setItems(studentRight);
		
	}
	@FXML
	public void deleteAction(ActionEvent event) {
		Student studentSelected = (Student)tableSelected.getSelectionModel().getSelectedItem();
		StudentDao dao = new StudentDao();
		studentLeft.remove(studentSelected);
		studentSelected.setTeacher(null);     //修改选中的student的属性
		studentRight.add(studentSelected);
		dao.update(studentSelected);          //更新数据库中的student属性
		
	}
	
	@FXML
	public void addAction(ActionEvent event) {
		Student studentSelected = (Student)tableOption.getSelectionModel().getSelectedItem();
		StudentDao dao = new StudentDao();
		TeacherDao tea = new TeacherDao();
		Set<Student> set = null;
		if(teacher.getStudents() != null) {
			set = teacher.getStudents();
		}else {
			set = new HashSet<>();
		}
		studentRight.remove(studentSelected);
	    studentSelected.setTeacher(teacher);  //修改选中的student的属性
	  	studentLeft.add(studentSelected);
        dao.update(studentSelected);          //更新数据库中的student属性
        
        set.add(studentSelected);             //添加学生进入teacher
		teacher.setStudents(set);
		tea.update(teacher);                  //更新数据库中的老师
	}
}
